<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="code" label="货物编码">
        <el-input v-model="dataForm.code" placeholder="请输入货物编码"></el-input>
      </el-form-item>
      <el-form-item prop="status" label="货位状态">
        <el-radio-group v-model="dataForm.status">
          <el-radio value="ENABLED" label="可用"></el-radio>
          <el-radio value="UNENABLED" label="不可用"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="segmentNo" label="密集架">
        <el-input v-model="dataForm.segmentNo" placeholder="请输入密集架"></el-input>
      </el-form-item>
      <el-form-item prop="lineNo" label="行">
        <el-input v-model="dataForm.lineNo" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item prop="columnNo" label="列">
        <el-input v-model="dataForm.columnNo" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item prop="layerNo" label="层">
        <el-input v-model="dataForm.layerNo" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item prop="locationStatus" label="货位状态">
        <el-radio-group v-model="dataForm.locationStatus">
          <el-radio value="EMPTY" label="空"></el-radio>
          <el-radio value="UNEMPTY" label="非空"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="inoutLock" label="出入库锁">
        <el-input v-model="dataForm.inoutLock" placeholder="请输入"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils"; // isEmail, isMobile, isNumber,
// import { IObject } from "@/types/interface";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      //   roleList: [] as IObject,
      //   tenantList: [] as IObject[],
      dataForm: {
        warehouseId: "",
        id: "",
        code: "",
        status: "",
        segmentNo: "",
        lineNo: "",
        columnNo: "",
        layerNo: "",
        locationStatus: "",
        inoutLock: ""
      }
    });
  },
  computed: {
    dataRule() {
      return {
        code: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        status: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        segmentNo: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        lineNo: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        columnNo: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        layerNo: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        locationStatus: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        inoutLock: [{ required: true, message: this.$t("validate.required"), trigger: "change" }]
      };
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        this.getInfo();
      });
    },

    getInfo() {
      if (this.dataForm.id != "" && this.dataForm.id != undefined) {
        baseService.get(`/archive/warehouse-location/${this.dataForm.id}`).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        });
      }
    },

    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/archive/warehouse-location", {
          ...this.dataForm
        }).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>

<style lang="less">
.mod-tenant__tenant {
  .role-list {
    .el-select {
      width: 100%;
    }
  }
}
</style>
